package com.flute.haflute.monitor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.flute.haflute.common.ClusterContext;

public class LocalStatusMonitor {
	private static Logger logger = LoggerFactory.getLogger(LocalStatusMonitor.class);
	static ConcurrentMap<String, StatusMessage> stmsgMap = new ConcurrentHashMap<String, StatusMessage>();
	static int time  =  ClusterContext.getMonitorMsgAge();
	
	public void  reportStatusMessage(Map<String, StatusMessage> map){
		Iterator<String> it =   map.keySet().iterator();
		while(it.hasNext()){
			String key = it.next();
			StatusMessage statusMessage = map.get(key);
			String topickey = key + "^" + map.get(key).topic;
			if(!statusMessage.topic.equalsIgnoreCase("Jvm")){
				logger.debug(" LocalStatusMonitor status info :" + statusMessage.topic);
			}
			LocalStatusMonitor.stmsgMap.put(topickey,statusMessage);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				long reciiveTime = df.parse(statusMessage.reportDate_JobReport.toString()).getTime();
				long currentTime = System.currentTimeMillis();
				int time  =  ClusterContext.getMonitorMsgAge();
				if((currentTime-reciiveTime)/1000/60>time*100){
					stmsgMap.remove(key);
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
		}
	}
	public static Map<String, StatusMessage> getJvmMap() {
		return stmsgMap;
	}
	
			
}
